/**
* 
* event for task
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* Classe: penalityTrace.java 
* see <a href="{@docRoot}/documentation/trace.html">Trace for more information</a>
*  
* @author: Urunuela Richard
* @version 3.2
*/

package log;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

//import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

import storm.ISimulationKernel;
import storm.Processors.Processor;
import storm.Tasks.Task;

public class TaskTrace extends Trace {
	
	transient public Processor CPU;
	transient public Task task;
	public int blocksize=0;
	public int RET;
	public int VRET;
	public Task.States state;
	public ArrayList ownfieldKey;
	public ArrayList ownfieldValue;
	/**
	 * 
	 * @author richard
	 *
	 */
	public enum Type{
		END,
		ACTIVATION,
		UB_B_LOCK,
		RET, FAILED, ABORT, PENALITY, RUNNING, SETRUNNING, SETREADY, SETWAITING, SETUNEXISTING, SETABORT, SETRUNNINGON, SETPREEMPT,
	}
	public TaskTrace.Type type;
	private int numCpu;
	private int numTask;
	public String totxtFile()	{
		return (CPU.toString()+" " + this.date + " " + (this.date +1) +" " + this.task.toString());
}
	/**
	 * 
	 */
	private void setOwnfield(){
		ownfieldKey = new ArrayList();
		ownfieldValue= new ArrayList();
		 java.util.Hashtable ref = this.task.getOwnField();
		    Iterator itValue = ref.values().iterator(); 
		    Iterator itKey = ref.keySet().iterator();
		    while(itValue.hasNext()){ 
		        String  value = itValue.next().toString();
		        String key = itKey.next().toString();
		        ownfieldKey.add(key);
		        ownfieldValue.add(value);
		    
		   
		      } 
		
	}
	public TaskTrace(){
	
	}
	/**
	 * 
	 * @param n date 
	 * @param T Task 
	 * @param type type of event END|ACTIVATION|BLOCK | UNBLOCK ....
	 * @param block block compteur (=0 if not blocked)
	 */
	public TaskTrace(int n , Task T,TaskTrace.Type type, int block){
		this(n,T,type);
		this.blocksize = block;
		this.state = T.getState();
		this.CPU = T.getProcessor();
		
		setOwnfield();
	}
	/**
	 * 
	 * @param n date
	 * @param T task 
	 * @param type type of trace
	 */
	public TaskTrace(int n , Task T,TaskTrace.Type type){
		this.date = n;

		this.task= T;
		this.type = type;
		this.state = T.getState();
		this.CPU = T.getProcessor();
		setOwnfield();
	}

/**
 * 
 * @return
 */
	public Processor getCPU() {
		return CPU;
	}
	/**
	 * 
	 * @param cpu
	 */
	public void setCPU(Processor cpu) {
		CPU = cpu;
	}
	/**
	 * 
	 * @return
	 */
	public Task getTask() {
		return task;
	}
	/**
	 * 
	 * @param task
	 */
	public void setTask(Task task) {
		this.task = task;
	}
	/**
	 * 
	 * @return
	 */
	public boolean isEnd() {
		if (this.type == TaskTrace.Type.END) return true ;
		else return false;
}
	/**
	 * 
	 * @return
	 */
	public boolean isActivation() {
		if (this.type == TaskTrace.Type.ACTIVATION) return true ;
		else return false;
	}
	/**
	 * 
	 * @return
	 */
	public boolean isUBBlock() {
		if (this.type == TaskTrace.Type.UB_B_LOCK) return true ;
		else return false;
	}
	/**
	 * 
	 * @return
	 */
	public String getBlock() {
		return (this.task.blockSize()+"");
	}
	/**
	 * 
	 * @return
	 */
	public boolean isRET() {
		if (this.type == TaskTrace.Type.RET) return true ;
		return false;
	}
	/**
	 * 
	 * @return
	 */
	public boolean isfailed() {
		if (this.type == TaskTrace.Type.FAILED) return true ;
		else return false;
	}
	/**
	 * 
	 * @return
	 */
	public boolean isabort() {
		if (this.type == TaskTrace.Type.ABORT) return true ;
		else return false;
	}
	/**
	 * 
	 * @return
	 */
	public boolean isPenality() {
		if (this.type == TaskTrace.Type.PENALITY) return true ;
		else return false;
	}
	
	
	/***
	 * 
	 * Serialisation
	 * @param out
	 * @throws IOException
	 */
	private void writeObject(ObjectOutputStream out) throws IOException {
		// appel des m�canismes de s�rialisation par d�faut
		out.defaultWriteObject();


		if (CPU != null ) out.writeInt(CPU.id);
		else out.writeInt(-1);
		out.writeInt(task.id);


	}
	public void reset(ISimulationKernel kernel) {
		// TODO Auto-generated method stub
		if (numCpu > -1 ) this.CPU = kernel.getTasksListeManager().getProcessorsById(this.numCpu);
		else this.CPU = null;
		this.task = kernel.getTasksListeManager().getTaskbyId(this.numTask);
		
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
		

		numCpu  =  in.readInt();
		numTask  =  in.readInt();

	

		
	}


	
	
}
